/* 这里写事件的接口 */

var express = require("express")
var router = express.Router()

/* 导入增删改查的方法 */
const { exec, getAll, getOne } = require("../../db")

/* 添加事件,就算是管理员角色去添加事件，一开始添加的事件状态也是未审核的状态，就不用传这个status的值 */
/* post ,添加数据，
用户Id, 事件类型（下拉框选择），事件描述(也是下拉框，显示的是文字，比如严重，不严重，但是传过来的是数字)，
事件等级，发生地址，车牌号，经纬度 */
router.post("/back/event", async (req, res) => {
  /* 从请求体中解构参数 */
  let { user_id, type, description, grade, address, car_number, lnglat } =
    req.body
  let sql = `insert into eventinfo
   (user_id,type,description,grade,address,car_number,lnglat)
   values ('${user_id}','${type}','${description}','${grade}','${address}','${car_number}','${lnglat}')`
  try {
    let data = await exec(sql)
    sql = `select * from eventinfo where id=${data.insertId}`
    data = await getOne(sql)
    res.send({
      code: 200,
      message: "success",
      result: data,
    })
  } catch (err) {
    console.log(err)
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 分页查询事件所有没有处理的事件，就是event_status的值为0 */
router.get("/back/getallevent", async (req, res) => {
  const { page, size } = req.query
  /* 获得总数 */
  let sql = `select count(*) as total from eventinfo where event_status=0`
  let { total } = await getOne(sql)
  let offset = (page - 1) * size
  sql = `select * from eventinfo where event_status=0 order by submit_time desc LIMIT ${offset}, ${size}`
  try {
    let data = await getAll(sql)
    res.send({
      code: 200,
      message: "success",
      result: {
        total,
        data,
      },
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})
/* 更改事件状态,通过
在更改状态的页面中显示的是所有没有处理过的事件 */
router.put("/back/passevent/:id", async (req, res) => {
  let { id } = req.params
  let sql = `update eventinfo set event_status=2 where id='${id}' `
  try {
    await exec(sql)
    res.send({
      code: 200,
      message: "success",
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 更改事件状态,忽略 */
router.put("/back/ignoreevent/:id", async (req, res) => {
  let { id } = req.params
  let sql = `update eventinfo set event_status=1 where id='${id}'`
  try {
    await exec(sql)
    res.send({
      code: 200,
      message: "success",
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

/* 折线图的数据接口，统计最近7天每天发生的事件的个数 */
router.get("/back/eventline", async (req, res) => {
  let sql = `select d.date,IFNULL(e.number,0)  number from (SELECT CURDATE() AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS date
    UNION ALL
    SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS date) d LEFT OUTER JOIN (SELECT count(*) number,FROM_UNIXTIME(UNIX_TIMESTAMP(submit_time),'%Y-%m-%d') happeneddate from eventinfo group by  happeneddate ) e
on d.date=e.happeneddate order by d.date asc`
  try {
    let data = await getAll(sql)
    res.send({
      code: 200,
      message: "success",
      result: data,
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})
/* 饼状图的数据接口，统计各个类别的事件的个数 */
router.get("/back/eventpie", async (req, res) => {
  let sql = `SELECT count(*) value,type name from eventinfo group by  type `
  try {
    let data = await getAll(sql)
    res.send({
      code: 200,
      message: "success",
      result: data,
    })
  } catch (err) {
    res.send({
      code: 201,
      message: "fail",
      result: "",
    })
  }
})

module.exports = router
